<template>
  <el-card class="statistics-access">
    <div slot="header" class="clearfix">
      <card-header title="访问量" :icon="require('@/assets/index/jurassic.png')" @change="changeOrderTime" />
    </div>
    <ve-line
      :colors="colors"
      :data="chartData"
      :data-empty="dataEmpty"
      :extend="extendsConfig"
      :loading="loading"
      :settings="chartSettings"
    />
  </el-card>
</template>

<script>
import VeLine from 'v-charts/lib/line.common';
import echarts from 'echarts';
import CardHeader from '../card_header.vue';
import 'v-charts/lib/style.css';

export default {
  components: {
    VeLine,
    CardHeader
  },
  data() {
    return {
      time: 'today',
      loading: true,
      dataEmpty: true,
      chartSettings: {
        labelMap: {
          date: '日期',
          pv: '访问量(PV)',
          uv: '独立访客数(UV)'
        },
        xAxisType: 'time',
        yAxisType: ['KMB'],
        area: true,
        scale: [true, false]
      },
      chartData: {
        columns: ['date', 'pv', 'uv'],
        rows: []
      },
      colors: [
        // 颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
        new echarts.graphic.LinearGradient(0, 1, 0, 0, [
          {
            offset: 0,
            color: '#C8E3FF'
          },
          {
            offset: 0.6,
            color: '#C8E7FF'
          }
        ]),
        new echarts.graphic.LinearGradient(0, 1, 0, 0, [
          {
            offset: 0,
            color: '#FFEAD6'
          },
          {
            offset: 0.6,
            color: '#FFDBA8'
          }
        ])
      ],
      extendsConfig: {
        grid: {
          right: 20
        },
        xAxis(v) {
          v.forEach(i => {
            i.boundaryGap = false;
            i.axisLabel = {
              showMaxLabel: true
            };
          });

          return v;
        },
        yAxis(v) {
          v.forEach(i => {
            i.minInterval = 1;
          });

          return v;
        }
      }
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    changeOrderTime: function (time) {
      this.time = time;
      this.loadData();
    },
    loadData: function () {
      this.$http.get(
          '/admin/data/website/visit',
          {
            time: this.time
          },
          response => {
            const data = response.data.rows;
            if (data.length == 0) {
              this.dataEmpty = true;
              this.chartData.rows = [];
            } else {
              this.dataEmpty = false;
              if (this.time == 'today') {
                const now = new Date();
                const year = now.getFullYear(); // 年
                const month = now.getMonth() + 1; // 月
                const day = now.getDate(); // 日
                data.forEach(v => {
                  v.date = year + '-' + month + '-' + day + ' ' + v.date;
                });
              }
              this.chartData.rows = data;
            }

            this.loading = false;
          },
          _ => {
            this.loading = false;
          },
          false
      );
    }
  }
};
</script>

<style lang="scss" scoped>
.statistics-access {
  height: 460px;

  ::v-deep.el-card__body {
    // padding: 0 40px 0 20px;
  }
}
</style>
